/*
 * Copyright (C) 2010-2012 Enrique Lara (enrique.posix2.lara@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
 * limitations under the License.
  */
package trs.wmata.api;

import app.transitalarm.util.Log;

import java.io.Serializable;
import java.lang.ref.SoftReference;
import java.util.HashMap;

public final class Cache {
    private static final String TAG = "Cache";
    private static final HashMap<String, SoftReference<CacheEntry>> CACHE = new HashMap<String, SoftReference<CacheEntry>>();

    static CacheEntry addToCache(final String url, final String content, final int responseCode, final String responseMessage) {
        final CacheEntry entry = new CacheEntry(content, responseCode, responseMessage);
        CACHE.put(url, new SoftReference<CacheEntry>(entry));

        return entry;
    }

    public static CacheEntry addToCache(final String cacheKey, final Serializable object) {
        final CacheEntry entry = new CacheEntry(object);
        CACHE.put(cacheKey, new SoftReference<CacheEntry>(entry));

        return entry;
    }

    static CacheEntry getFromCache(final String url) {
        CacheEntry entry = null;

        if (CACHE.containsKey(url)) {
            final SoftReference<CacheEntry> ref = CACHE.get(url);
            entry = ref.get();

            if (entry == null) {
                Log.d(TAG, "Cache Soft MISS," + url);
                CACHE.remove(url);
            } else {
                Log.d(TAG, "Cache Entry HIT," + url);
            }
        } else {
            Log.d(TAG, "Cache MISS     ," + url);
        }

        return entry;
    }

    static CacheEntry removeFromCache(final String url) {
        CacheEntry entry = null;

        if (CACHE.containsKey(url)) {
            final SoftReference<CacheEntry> ref = CACHE.remove(url);
            entry = ref.get();
        }

        return entry;
    }

//XXX add "request count" and/or "cacheAddTime" to delete requests if they are too old
    static class CacheEntry {
        private final Object object;
        private final String content;
        private final int responseCode;
        private final String responseMessage;

        private CacheEntry(final String content, final int responseCode, final String responseMessage) {
            object = "";
            this.content = content;
            this.responseCode = responseCode;
            this.responseMessage = responseMessage;
        }

        private CacheEntry(final Object object) {
            this.object = object;
            content = "";
            responseCode = 200;
            responseMessage = "";
        }

        public Object getObject() {
            return object;
        }

        public String getContent() {
            return content;
        }

        public int getResponseCode() {
            return responseCode;
        }

        public String getResponseMessage() {
            return responseMessage;
        }
    }
}
